package com.bittech.servlet.smallfileserver.dao;

import com.bittech.servlet.smallfileserver.dao.impl.DatabaseSmallDaoImpl;
import com.bittech.servlet.smallfileserver.dao.impl.FileSystemSmallDaoImpl;
import com.bittech.servlet.smallfileserver.dao.impl.InMemorySmallDaoImpl;
import com.bittech.servlet.smallfileserver.dao.impl.MongodbSmallDaoImpl;
import com.bittech.servlet.smallfileserver.dao.impl.OssSmallDaoImpl;

/**
 * Author: secondriver
 * Created: 2018/10/21
 */
public class StorageModeFactory {
    
    
    private static SmallFileDao smallFileDao;
    
    private static StorageMode storageMode = StorageMode.Memory;
    
    public synchronized static SmallFileDao getSmallDao(StorageMode mode) {
        if (storageMode == mode && smallFileDao != null) {
            return smallFileDao;
        } else {
            storageMode = mode;
            switch (mode) {
                case OSS:
                    smallFileDao = new OssSmallDaoImpl();
                    break;
                case File:
                    smallFileDao = new FileSystemSmallDaoImpl();
                    break;
                case MySQL:
                    smallFileDao = new DatabaseSmallDaoImpl();
                    break;
                case MongoDB:
                    smallFileDao = new MongodbSmallDaoImpl();
                    break;
                case Memory:
                default:
                    smallFileDao = new InMemorySmallDaoImpl();
                    break;
            }
        }
        return smallFileDao;
    }
}
